package org.buweb.practice;

import java.text.DateFormat;
import java.util.*;

public class Person implements Comparable {
    
    public Person(String fn, String ln, Date bd, String hTown) {
        firstName = fn;
        lastName = ln;
        birthdate = bd;
        homeTown = hTown;
    }

    private String firstName;
    private String lastName;
    private Date birthdate;
    private String homeTown;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public String getHomeTown() {
        return homeTown;
    }

    public void setHomeTown(String homeTown) {
        this.homeTown = homeTown;
    }
    
    public String toString() {
        boolean added = false;
        StringBuffer sb = new StringBuffer();
        if (firstName != null) {
            added = true;
            sb.append("My name is ");
            sb.append(firstName);
            if (lastName != null) {
                sb.append(" ");
                sb.append(lastName);
            }
        }
        if (birthdate != null) {
            if (added) sb.append(", ");
            added = true;
            sb.append("I was born on ");
            DateFormat df = DateFormat.getDateInstance();
            sb.append( df.format(birthdate) );
            //sb.append(birthdate);
        }
        if (homeTown != null) {
            if (added) sb.append(", ");
            added = true;
            sb.append("I'm from ");
            sb.append(homeTown);
        }
        if (added) sb.append(".");
        return sb.toString();
    }

    /*
     * @returns a negative integer, zero, or a positive integer as this object 
     * is less than, equal to, or greater than the specified object. 
     */
    public int compareTo(Object o) {
        String n1 = getNameForCompare( this );
        String n2 = getNameForCompare( (Person) o );
        return n1.compareTo(n2);
    }
    
    private String getNameForCompare(Person p) {
        StringBuffer sb = new StringBuffer();
        if (p.getLastName() != null) {
            sb.append(p.getLastName() + " " );
        }
        if (p.getFirstName() != null) {
            sb.append(p.getFirstName());
        }
        return sb.toString();
    }
    
}
